﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Configuration;
namespace Common
{
    /// <summary>
    /// 邮件相关函数
    /// </summary>
    /// <remarks>
    /// <para>Fuyuanzhao</para>
    /// <para>2013.05.17</para>
    /// </remarks>
   public class EmailHelper
    {
        #region 发送电子邮件
        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="smtpserver">SMTP服务器</param>
        /// <param name="userName">登录帐号</param>
        /// <param name="pwd">登录密码</param>
        /// <param name="nickName">发件人昵称</param>
        /// <param name="strfrom">发件人</param>
        /// <param name="strto">收件人</param>
        /// <param name="subj">主题</param>
        /// <param name="bodys">内容</param>
        public static void sendMail(string smtpserver, string userName, string pwd, string nickName, string strfrom, string strto, string subj, string bodys)
        {
            try
            {
                SmtpClient _smtpClient = new SmtpClient();
                _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
                _smtpClient.Host = smtpserver;//指定SMTP服务器
                if (!string.IsNullOrEmpty(userName))
                {
                    _smtpClient.Credentials = new System.Net.NetworkCredential(userName, pwd);//用户名和密码
                }
                else
                {
                    _smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
                }
                //MailMessage _mailMessage = new MailMessage(strfrom, strto);
                MailAddress _from = new MailAddress(strfrom, nickName);
                MailAddress _to = new MailAddress(strto);
                MailMessage _mailMessage = new MailMessage(_from, _to);
                _mailMessage.Subject = subj;//主题
                _mailMessage.Body = bodys;//内容
                _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
                _mailMessage.IsBodyHtml = true;//设置为HTML格式
                _mailMessage.Priority = MailPriority.Normal;//优先级
                _smtpClient.Send(_mailMessage);
                //测试是否能发送成功
                //Common.EmailHelper.sendMail("172.17.10.17","","","fyz","fyz5897@arcsfot.com","yuanzhaofu01@qq.com","test","test1");
            }
            catch { }
        }

        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="smtpserver">SMTP服务器</param>
        /// <param name="userName">登录帐号</param>
        /// <param name="pwd">登录密码</param>
        /// <param name="nickName">发件人昵称</param>
        /// <param name="strfrom">发件人</param>
        /// <param name="strto">收件人</param>
        /// <param name="subj">主题</param>
        /// <param name="bodys">内容</param>
        public static void sendMail(string subj, string bodys)
        {
            string EmailService = ConfigurationManager.AppSettings["EmailService"];
            string EmailUserID = ConfigurationManager.AppSettings["EmailUserID"];
            string EmailPwd = ConfigurationManager.AppSettings["EmailPwd"];
            string ErrorRcvEmail = ConfigurationManager.AppSettings["ErrorRcvEmail"];
            string fromUser = ConfigurationManager.AppSettings["fromUser"];
            string fromEmail = ConfigurationManager.AppSettings["fromEmail"];

          
            //测试是否能发送成功
            try
            {
                Common.EmailHelper.sendMail(EmailService, EmailUserID, EmailPwd, fromUser, fromEmail, ErrorRcvEmail, subj, bodys);
            }
            catch
            { 
            }
        }
        #endregion

    }
}
